<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Role_model extends MY_Model
{
    private $table = 'role';
	public function __construct()
	{
		parent::__construct($this->table);
	
	}
	/**
	 * 获取角色列表
	 */
	public function getRoleList($select = '*', $limit = 10, $start = 0)
	{		
		$this->db->select($select);
		$this->db->order_by('roleId', 'asc')->limit($limit, $start);
		return $this->db->get($this->table)->result_array();
	}
	
	/**
	 * 添加
	 */
	public function add($roleData)
	{
		$bool = $this->db->insert($this->table, $roleData);
		$res = $this->db->insert_id();
		return $res;
	}
	
	
	/**
	 * 修改
	 */
	public function update($roleId, $data)
	{
		$this->db->where('roleId', $roleId);
		$query = $this->db->update($this->table, $data);
		return $this->db->affected_rows();
	}

	/**
	 * 查询单条数据
	 */
	public function getRoleRow($roleId = 0)
	{
		$this->db->where('roleId', $roleId);
		return $this->db->get($this->table)->row_array();
	}
	
	/**
	 * 检查角色名称是否存在
	 */
	public function checkUnique($roleName, $roleId = 0)
	{
		$this->db->from($this->table);
		$this->db->where('roleName', $roleName);
		if (!empty($roleId))
		{
			$this->db->where('roleId !=', $roleId);
		}
	
		$result = $this->db->get();
		return $result->row_array();
	}
	
	/**
	 * 获取总条数
	 */
	public function getRowCount($where = array())
	{
		$param = $this->_getWhere($where);
		$this->db->where($param);
		return $this->db->count_all_results($this->table);
	}
	
	/**
	 * 查询条件
	 */
	public function _getWhere($where = array())
	{
		$param = array();
		if (!empty($where['roleName']))
		{
			$param['roleName like'] = '%' . strval($where['roleName']) . '%';
		}
		return $param;
	}
	
	/**
	 * 删除
	 * @return bool
	 * */
	public function del( $roleId )
	{
	    $this->db->where('roleId', $roleId);
	    $this->db->delete( $this->table );
	    return $this->db->affected_rows();
	}
	
}